﻿using MediatR;
using Org.BouncyCastle.Ocsp;
using ProductionManagement.Application.Command.ScrapsCommand;
using ProductionManagement.Domain.Dtos;
using ProductionManagement.Domain.ProductionDomain;
using ProductionManagement.Dto;
using ProductionManagement.Infrstructre.lnterface;
using SqlSugar;

namespace ProductionManagement.Application.CommandHandler.ScrapsCommandHandler
{
    /// <summary>
    /// 废料的显示
    /// </summary>
    public class ListScrapsCommandHandler : IRequestHandler<ListScrapsCommand, PageDto<ListScrapsDto>>
    {

        IProductionWasteRepository pwRep;
        IProduceScrapsProductsRepository pspRep;
        ISqlSugarClient db;

        public ListScrapsCommandHandler(IProductionWasteRepository pwRep,
        IProduceScrapsProductsRepository pspRep, ISqlSugarClient db)
        {
            this.pwRep = pwRep;
            this.pspRep = pspRep;
            this.db = db;
        }

        public async Task<PageDto<ListScrapsDto>> Handle(ListScrapsCommand request, CancellationToken cancellationToken)
        {
            var list = db.Queryable<ProductionWaste>()
                .InnerJoin<ProduceScrapsProducts>((pw, psp) => pw.ProductionWasteNumber == psp.ProductionWasteNumber)
                .Where((pw, psp) => pw.IsDel == false)
                .GroupBy((pw, psp) => new
                {
                    pw.PWId,
                    pw.ProductionWasteNumber,
                    pw.ProductionWasteTopic,
                    pw.PDocumentNumber,
                    pw.ProductionDate,
                    pw.PIsStorage,
                    pw.PDocumentMakers,
                    pw.PRemark,
                    //psp.PScrapsNumber,
                  //  psp.ProductId,
                 //   psp.PSPId
                })
                .Select((pw, psp) => new ListScrapsDto
                {
                    PWId = pw.PWId,
                    ProductionWasteNumber = pw.ProductionWasteNumber,
                    ProductionWasteTopic = pw.ProductionWasteTopic,
                    PDocumentNumber = pw.PDocumentNumber,
                    ProductionDate = pw.ProductionDate,
                    PIsStorage = pw.PIsStorage,
                    PDocumentMakers = pw.PDocumentMakers,
                    PRemark = pw.PRemark,
                    PScrapsNumber =SqlFunc.AggregateSum(psp.PScrapsNumber),
                //    ProductId=psp.ProductId,
                    AddName = pw.AddName,
                  //  PSPId=psp.PSPId
                }).ToList();

            if (!string.IsNullOrEmpty(request.ProductionWasteNumber))
            {
                list = list.Where(x => x.ProductionWasteNumber.Contains(request.ProductionWasteNumber)).ToList();
            }
            if (!string.IsNullOrEmpty(request.ProductionWasteTopic))
            {
                list = list.Where(x => x.ProductionWasteTopic.Contains(request.ProductionWasteTopic)).ToList();
            }
            if (request.SProductionDate!=null||request.EProductionDate!=null)
            {
                list = list.Where(x => x.ProductionDate >= request.SProductionDate && x.ProductionDate <= request.EProductionDate).ToList();
            }
            if (!string.IsNullOrEmpty(request.AddName))
            {
                list = list.Where(x => x.AddName.Contains(request.AddName)).ToList();
            }
            PageDto<ListScrapsDto> page = new PageDto<ListScrapsDto>();
            page.lcount = list.Count();
            page.pcount = (int)Math.Ceiling(page.lcount * 1.0 / request.Psize);
            page.list = list.OrderByDescending(x => x.PWId).Skip((request.Pindex - 1) * request.Psize).Take(request.Psize).ToList();

            return page;
        }

    }
}
